import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    children:[
      {
        path:"/shou",
        component: () => import('../views/page/Home.vue'),
      },
      {
        path:"/fen",
        component: () => import('../views/page/Fen.vue'),
      },
      {
        path:"/shop",
        component: () => import('../views/page/Shop.vue'),
      },
      {
        path:"/my",
        component: () => import('../views/page/My.vue'),
        meta:{islogin:true}
      },
    ]
  },
  {
    path:'/detail/:id',
    component: () => import('../views/Detail.vue'),
  },
  {
    path:'/login',
    component: () => import('../views/Login.vue'),
  },
  {
    path:'*',
    component: () => import('../views/Erroy.vue'),
  },
  {
    path:'/',
    redirect: '/shou',  // 重定向:重新指向其它path,会改变网址
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// if(window.localStorage.getItem("token"))
// {
//   children.forEach(item => {
//     router.addRoute("index",item)
//   });
// }
// 路由守卫
// const white=["/login","/fen"]
// router.beforeEach((to, from,next) => {
//   if(token||white.includes(to.path))
//   {
//     next()
//   }else{
//     next("/login")
//   }
// })
router.beforeEach((to, from, next) => {
  if (to.meta.islogin && !localStorage.getItem("token")){
    next("/login")
  }else{
    next()
  } 
})

export default router
